package com.nd.android.musicboxlibrary.business.db.parsers;

import java.io.File;

import android.content.ContentValues;
import android.database.Cursor;

import com.common.StarAppConfig;
import com.nd.android.musicboxlibrary.business.bean.MusicInfo;
import com.nd.android.musicboxlibrary.business.db.table.MusicColumns;

/**
 * 认证信息DB解析类
 * 
 * @author 慕容秋 (muroqiu@qq.com)
 */
public class MusicDbParser implements DbParser<MusicInfo> {

	@Override
	public MusicInfo parse(Cursor dbCursor) {
		MusicInfo obj = new MusicInfo();
		obj.setId(dbCursor.getLong(dbCursor.getColumnIndex(MusicColumns.FID)));
		obj.setHQId(dbCursor.getLong(dbCursor.getColumnIndex(MusicColumns.HQFID)));// by jiangwb 2014-11-18 10:49
		obj.setMusicName(dbCursor.getString(dbCursor.getColumnIndex(MusicColumns.NAME)));
		obj.setPath(dbCursor.getString(dbCursor.getColumnIndex(MusicColumns.PATH)));
		obj.setLrcPath(dbCursor.getString(dbCursor.getColumnIndex(MusicColumns.LRCPATH)));
		obj.setTime(dbCursor.getString(dbCursor.getColumnIndex(MusicColumns.TIME)));
		obj.setHasMv(dbCursor.getInt(dbCursor.getColumnIndex(MusicColumns.HASMV)) == 1);
		obj.setHQ(dbCursor.getInt(dbCursor.getColumnIndex(MusicColumns.ISHQ)) == 1);
		obj.setNamePy(dbCursor.getString(dbCursor.getColumnIndex(MusicColumns.NAMEPY)));
		obj.setLrcId(dbCursor.getLong(dbCursor.getColumnIndex(MusicColumns.LRCID)));
		obj.setSize(dbCursor.getInt(dbCursor.getColumnIndex(MusicColumns.SIZE)));
		obj.setHQSize(dbCursor.getInt(dbCursor.getColumnIndex(MusicColumns.HQSIZE)));// by jiangwb 2014-11-18 10:49
		obj.setSizeDownLoaded(dbCursor.getInt(dbCursor.getColumnIndex(MusicColumns.SIZEDOWNLOADED)));
		obj.setAlbumId(dbCursor.getLong(dbCursor.getColumnIndex(MusicColumns.ALBUMID)));
		obj.setSpecialName(dbCursor.getString(dbCursor.getColumnIndex(MusicColumns.ALBUMNAME)));
		obj.setBgFid(dbCursor.getLong(dbCursor.getColumnIndex(MusicColumns.BGFID)));
		obj.setFaved(dbCursor.getLong(dbCursor.getColumnIndex(MusicColumns.ORDERFAV)) > 0);
		/*
		if(!StarAppConfig.getBooleanConfig(StarAppConfig.CONFIG_VERSION1_8)){// by jiangwb
			obj.setDownLoaded(obj.getSizeDownLoaded() == obj.getSize());
		}else{
			//先判断是否有HQ的，在判断是否有标清的，再没有，就是没有下载咯
			//obj.setDownLoaded(new File(obj.getHQMusicPath()).isFile()? true: (!obj.isHQ() && new File(obj.getMusicPath()).isFile()));
			obj.setDownLoaded(false);
			if(obj.getSizeDownLoaded() > 0){//说明数据库有对这个文件，做下载记录
				if(obj.isHQ() && obj.getHQId() > 0 && new File(obj.getHQMusicPath()).isFile()){
					obj.setDownLoaded(true);
				}else{
					obj.setDownLoaded(new File(obj.getMusicPath()).isFile());
				}
			}
		}
		*/
		// 是否已经下载，不做版本区分，by jiangwb
		if(obj.getSizeDownLoaded() > 0){//说明数据库有对这个文件，做下载记录
			if(obj.isHQ() && obj.getHQId() > 0 && new File(obj.getHQMusicPath()).isFile()){
				obj.setDownLoaded(true);
			}else{
				obj.setDownLoaded(new File(obj.getMusicPath()).isFile());
			}
		}else{
			obj.setDownLoaded(false);
		}
		
//		obj.setDownLoading(dbCursor
//				.getLong(dbCursor.getColumnIndex(MusicColumns.ORDERDOWNLOAD)) > 0
//				&& (new File(obj.getHQMusicTemp()).isFile()
//						? true : new File(obj.getMusicTemp()).isFile()));
		//by jiangwb
		//		obj.setDownLoading(dbCursor.getLong(dbCursor.getColumnIndex(MusicColumns.ORDERDOWNLOAD)) > 0
		//				&& !obj.isDownLoaded());

				obj.setDownLoading(dbCursor.getLong(dbCursor.getColumnIndex(MusicColumns.ORDERDOWNLOAD)) > 0
						&& !obj.isDownLoaded());
		
		return obj;
	}

	@Override
	public ContentValues toContentValues(MusicInfo t) {
		ContentValues values = new ContentValues();
		values.put(MusicColumns.FID, t.getId());
		values.put(MusicColumns.NAME, t.getMusicName());
		//        values.put(MusicColumns.PATH, t.getMusicPath());
		//        values.put(MusicColumns.LRCPATH, t.getLrcPath());
		values.put(MusicColumns.TIME, t.getTime());
		if (t.isHasMv()) {
			values.put(MusicColumns.HASMV, 1);
		} else {
			values.put(MusicColumns.HASMV, 0);
		}
		if (t.isHQ()) {
			values.put(MusicColumns.ISHQ, 1);
		} else {
			values.put(MusicColumns.ISHQ, 0);
		}
		values.put(MusicColumns.NAMEPY, t.getNamePy());
		values.put(MusicColumns.LRCID, t.getLrcId());
		values.put(MusicColumns.SIZE, t.getSize());
		values.put(MusicColumns.HQSIZE, t.getHQSize());// by jiangwb 2014-11-18 10:48
		values.put(MusicColumns.HQFID, t.getHQId());// by jiangwb 2014-11-18 10:48
		//        values.put(MusicColumns.SIZEDOWNLOADED, t.getSizeDownLoaded());
		values.put(MusicColumns.ALBUMID, t.getAlbumId());
		if (t.isFaved()) {
			values.put(MusicColumns.ORDERFAV, System.currentTimeMillis());
		}

		return values;
	}
}
